<?php

namespace app\school\event\subscribes\school;

use app\admin\logic\system\Log;
use think\Event;
use think\facade\Db;

class SchoolLog
{
    public function subscribe(Event $event)
    {
        $admin = '管理员【' . request()->user->user_name . '/' . request()->user->id . '】';

        /** 新建 */
        $event->listen('SchoolCreated', function ($school) use ($admin) {

            Log::write($admin . '创建了机构【' . $school->title . '】', 'school', 'create');
        });

        /** 更新 */
        $event->listen('SchoolUpdated', function ($school) use ($admin) {
            $statusBefore = cache('school_' . $school->id . "_status_before_update");
            if ( $statusBefore == 2 && $statusBefore != $school->status ) {
                $status  = $school->status == 3 ? '驳回' : '通过';
                $content = $admin . $status . '了机构【' . $school->title . '】的审核';
            } else {
                $content = $admin . '修改了机构【' . $school->title . '】的信息';
            }
            Log::write($content, 'school', 'update');
        });

        /** 删除 */
        $event->listen('SchoolDeleted', function ($school) use ($admin) {
            Log::write($admin . '删除了机构【' . $school->title . '】', 'school', 'delete');
        });

    }
}
